package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.FitnesslogsMapper;
import com.ruoyi.system.domain.Fitnesslogs;
import com.ruoyi.system.service.IFitnesslogsService;

/**
 * 健身日志Service业务层处理
 * 
 * @author 张英旺
 * @date 2024-02-27
 */
@Service
public class FitnesslogsServiceImpl implements IFitnesslogsService 
{
    @Autowired
    private FitnesslogsMapper fitnesslogsMapper;

    /**
     * 查询健身日志
     * 
     * @param logId 健身日志主键
     * @return 健身日志
     */
    @Override
    public Fitnesslogs selectFitnesslogsByLogId(Long logId)
    {
        return fitnesslogsMapper.selectFitnesslogsByLogId(logId);
    }

    /**
     * 查询健身日志列表
     * 
     * @param fitnesslogs 健身日志
     * @return 健身日志
     */
    @Override
    public List<Fitnesslogs> selectFitnesslogsList(Fitnesslogs fitnesslogs)
    {
        return fitnesslogsMapper.selectFitnesslogsList(fitnesslogs);
    }

    /**
     * 新增健身日志
     * 
     * @param fitnesslogs 健身日志
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFitnesslogs(Fitnesslogs fitnesslogs)
    {
        fitnesslogs.setCreateTime(DateUtils.getNowDate());
        int rows = fitnesslogsMapper.insertFitnesslogs(fitnesslogs);
        insertSysUser(fitnesslogs);
        return rows;
    }

    /**
     * 修改健身日志
     * 
     * @param fitnesslogs 健身日志
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFitnesslogs(Fitnesslogs fitnesslogs)
    {
        fitnesslogs.setUpdateTime(DateUtils.getNowDate());
        fitnesslogsMapper.deleteSysUserByUserId(fitnesslogs.getLogId());
        insertSysUser(fitnesslogs);
        return fitnesslogsMapper.updateFitnesslogs(fitnesslogs);
    }

    /**
     * 批量删除健身日志
     * 
     * @param logIds 需要删除的健身日志主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFitnesslogsByLogIds(Long[] logIds)
    {
        fitnesslogsMapper.deleteSysUserByUserIds(logIds);
        return fitnesslogsMapper.deleteFitnesslogsByLogIds(logIds);
    }

    /**
     * 删除健身日志信息
     * 
     * @param logId 健身日志主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFitnesslogsByLogId(Long logId)
    {
        fitnesslogsMapper.deleteSysUserByUserId(logId);
        return fitnesslogsMapper.deleteFitnesslogsByLogId(logId);
    }

    /**
     * 新增用户信息信息
     * 
     * @param fitnesslogs 健身日志对象
     */
    public void insertSysUser(Fitnesslogs fitnesslogs)
    {
        List<SysUser> sysUserList = fitnesslogs.getSysUserList();
        Long logId = fitnesslogs.getLogId();
        if (StringUtils.isNotNull(sysUserList))
        {
            List<SysUser> list = new ArrayList<SysUser>();
            for (SysUser sysUser : sysUserList)
            {
                sysUser.setUserId(logId);
                list.add(sysUser);
            }
            if (list.size() > 0)
            {
                fitnesslogsMapper.batchSysUser(list);
            }
        }
    }
}
